package com.dk.controller;

import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.pojo.User;
import com.dk.service.IUserService;
import com.dk.shiro.LoginException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("userController")
@Api(value = "用户接口文档")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IUserService iUserService;

    @ApiOperation(value = "用户登录校验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uname", value = "用户名", required = true,paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true,paramType = "query")
    })
    @PostMapping
    public Result login(User user) {
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
         /*
          shiro 登录逻辑
         */
//      获取Subject
        Subject subject = SecurityUtils.getSubject();
//      封装用户数据
        UsernamePasswordToken token1 = new UsernamePasswordToken(user.getUname(), user.getPassword());
//      执行登录方法
        try {
            subject.login(token1); //交给shiro认证校验  >>>>>>>> realm 方法中执行验证逻辑
            String token = UUID.randomUUID().toString();
            token = token.replace("-", "");
            redisTemplate.boundValueOps(token).set(user);//将用户数据放入redis中
            redisTemplate.boundValueOps(token).expire(30, TimeUnit.MINUTES);//将用户数据放入redis中
            return Result.bulid(ConstantEnum.SUCCESS).setResult(token); //登录成功
        } catch (UnknownAccountException e) { //用户名不存在或者密码错误会抛出对应的异常
//            登录失败 : 用户名不存在
            return Result.bulid(ConstantEnum.SELLER_NOEXIST);
        } catch (LoginException e) { //用户名不存在或者密码错误会抛出对应的异常
//            登录失败 : 用户状态异常
            return Result.bulid(ConstantEnum.USER_ABNORMAL);
        } catch (IncorrectCredentialsException e) {
//            登录失败: 密码错误
            return Result.bulid(ConstantEnum.ERROR_PWD);
        }
    }

    //User表的增删改查
    @ApiOperation(value = "用户数据展示")
    @GetMapping
    public Result findAll() {
        List<User> list = iUserService.findAll();
        return Result.bulid(ConstantEnum.SUCCESS).setResult(list);
    }

    @ApiOperation(value = "用户增加操作")
    @PostMapping("insertInfo1")
    public Result insertInfo1(@RequestParam("rids") String[] rids,User user){
         boolean b =  iUserService.insertInfo(rids,user);
         return Result.bulid(ConstantEnum.SUCCESS).setResult(b);
    }

    @ApiOperation(value = "用户的修改操作")
    @PutMapping
    public  Result update(String [] rids,User user){
        boolean b =   iUserService.updateByUid(rids,user);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(b);
    }

    @ApiOperation(value = "用户删除操作")
    @DeleteMapping
    public Result deleteById(Long uid){
         boolean b =iUserService.deleteById(uid);
         return Result.bulid(ConstantEnum.SUCCESS).setResult(b);
    }
}
